<template>
  <div class="page-order-detail">
    <!-- 顶部导航 -->
    <van-nav-bar title="订单详情"
      left-text="返回"
      :right-text= "constants.ORDER_STATUS[orderDetail.status]"
      fixed
      left-arrow
      @click-left="goBack"/>
    <!-- 订单信息 -->
    <div class="order-message">
      <div class="order-message-title1">订单信息</div>
      <div class="order-info">
        <div class="order-number">
          <div class="order-number-zh">订单号</div>
          <div class="order-number-sz">{{ orderDetail.sn }}</div>
        </div>
        <div class="order-time">
          <div class="order-time-zh">下单时间</div>
          <div class="order-time-sz">{{ orderDetail.created_at }}</div>
        </div>
      </div>
      <div class="order-message-title2">订单明细</div>
      <div class="order-item" v-for="(item, index) in orderDetail.items" :key="index">
        <div class="order-item-head">商品{{ index + 1 }}</div>
        <div class="order-item-body">
          <div class="left">
            <van-image width="100" height="100" :src="item.flash_img"/>
          </div>
          <div class="right">
            <div class="title">{{ item.flash_name }}</div>
            <div class="price-count">
              <div class="count">数量：{{ item.count }}</div>
              <div class="price">价格：￥{{ item.amount }}</div>
            </div>
            <div class="gotime">{{ item.remark }}</div>
          </div>
        </div>
      </div>
      <!-- 订单总计 -->
      <div class="order-message-title3">订单总计</div>
      <div class="order-total">
        <div class="order-total-message">总计{{ orderDetail.buy_count }}件商品 合计￥{{ orderDetail.buy_amount }}</div>
        <van-button round size="small" type="warning" @click="orderDelete()"
        >删除订单</van-button>
        <van-button round size="small" type="primary"
            v-if="orderDetail.status == constants.ORDER_STATUS_PAY"
            @click="goPay()">去支付</van-button>
      </div>
    </div>
  </div>
</template>
<script>
import { ajax } from '@/utils/ajax'
import { OrderApis } from '@/utils/apis'
import * as constants from '@/utils/constants'
export default {
  data () {
    return {
      sn: '',
      orderDetail: {},
      constants
    }
  },
  methods: {
    /**
     * 删除订单
     */
    orderDelete () {
      this.$dialog.confirm({
        title: '温馨提示',
        message: '删除订单将无法恢复，确认删除订单？'
      }).then(() => {
      // 调用接口
        const url = OrderApis.orderDetailUrl.replace('#{sn}', this.sn)
        ajax.delete(url).then(res => {
        // 告诉用户已经删除成功了
        // 隐藏界面上的显
          if (res.status === 201) {
            this.$notify({
              type: 'success',
              message: '删除成功'
            })
            this.$router.push({name: 'OrderList', params: { status: constants.ORDER_STATUS_ALL}})
          }
        })
      })
    },
    /**
     * 跳转到支付页面
     */
    goPay () {
      this.$router.push({name: 'OrderPay', params: {sn: this.sn}})
    },
    goBack () {
      this.$router.go(-1)
    },
    getOrderDetail () {
      const url = OrderApis.orderDetail1Url.replace('#{sn}', this.sn)
      ajax.get(url).then(({ data }) => {
        this.orderDetail = data
      })
    }
  },
  mounted () {
    this.sn = this.$route.params.sn
    this.getOrderDetail()
  }
}
</script>
<style lang="less">
.page-order-detail {
  .order-message {
    .order-message-title1 {
      text-align: left;
      padding: 50px 10px 10px 10px;
    }
    .order-info {
        background-color: #ffffff;
      .order-number {
        padding: 15px;
        display: flex;
        justify-content: space-between;
        .order-number-sz {
          color: #999;
        }
      }
      .order-time {
        padding: 15px;
        display: flex;
        justify-content: space-between;
        .order-time-sz {
          color: #999;
        }
      }
    }
    .order-message-title2 {
      text-align: left;
      padding: 10px;
    }
    .order-item {
      background-color: #ffffff;
      .order-item-head {
        padding: 10px;
        font-size: 12px;
        text-align: left;
      }
      .order-item-body {
        padding: 10px;
        display: flex;
        .right {
          .title {
            font-size: 20px;
            padding: 5px 10px 5px 10px;
            text-align: left;
          }
          .price-count {
            display: flex;
            .count {
              padding: 5px 10px 5px 10px;
              font-size: 10px;
            }
            .price {
              padding: 5px;
              font-size: 10px;
            }
          }
          .gotime {
            padding: 0px 10px 0px 10px;
            font-size: 10px;
            text-align: left;
            color: #999;
          }
        }
      }
    }
    .order-message-title3 {
      padding: 10px;
      text-align: left;
    }
    .order-total {
      background-color: #ffffff;
      text-align: right;
      padding: 5px;
      .order-total-message {
        font-size: 12px;
        padding: 5px;
      }
      .van-button {
        margin-right: 5px;
      }
    }
  }
}
</style>
